package com.dries.security.core.validate.code.captcha.image;

import cn.hutool.core.util.IdUtil;
import com.dries.security.core.properties.SecurityCoreProperties;
import com.dries.security.core.validate.code.captcha.SysCaptcha;
import com.dries.security.core.validate.code.captcha.ValidateCodeGenerator;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import javax.annotation.Resource;

/**
 * @Description:
 * @Author ZSY
 * @createTime 2020/9/8 19:16
 */
@Component("imageValidateCodeGenerator")
public class ImageCodeGenerator implements ValidateCodeGenerator {

    @Resource
    private DefaultKaptcha captcha;

    @Resource
    private SecurityCoreProperties coreProperties;

    @Override
    public SysCaptcha generate(ServletWebRequest request) {
        String sessionCode = captcha.createText();
        return new SysCaptcha(IdUtil.simpleUUID(), sessionCode, coreProperties.getCode().getKaptcha().getExpireIn());

    }
}
